home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 401 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: solon.com!not-for-mail
  2. From: thads@csn.net (Thad Smith)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 15 Feb 1996 09:09:53 -0600
  6. Organization: T3 Systems
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4fvic1$ef1@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com>
  11. Reply-To: ThadSmith@acm.org
  12. NNTP-Posting-Host: solutions.solon.com
  13.  
  14. In article <4fstj7$2l6@solutions.solon.com>,
  15. Rune Huseby <rune.huseby@gpi.telemax.no> wrote:
  16. :I got a problem understanding the rules on integral promotion in 
  17. :the C language:
  18. :
  19. :Consider the following function:
  20. :
  21. :short test(short x1, short x2);
  22. :
  23. :int main(void)
  24. :{
  25. :  short result;
  26. :  result = test(1, 2);
  27. :  return 0;
  28. :}
  29. :
  30. :short test(short x1, short x2)
  31. :{
  32. :  short result;
  33. :  result = x1 + x2;  /* Warning:  '=' : conversion from 'int '                                 
  34. :                         to 'short ', possible loss of data */
  35. :  return result;
  36. :}
  37. :
  38. :My compiler (Microsoft Visual C++ 4.0), automagically converts 
  39. :my short-parameters to int's, even though all variables involved 
  40. :are short. 
  41.  
  42. The parameters for a prototyped function are not converted.  That is,
  43. x1 and x2 remain type short int.  
  44.  
  45. When they are used in most expressions which expect an int type they
  46. undergo integral promotion, regardless of the type of the other
  47. operands.  
  48.  
  49. :I know that the standard says that all arguments can 
  50. :be converted to the biggest 'type' of all the arguments, but is 
  51. :it correct that char's and short's always are promoted to int's, 
  52. :without regard to the other arguments in the expression?
  53.  
  54. It depends on the usage, but usually yes.  Some exceptions would be
  55. operand of sizeof, ++, --, and simple assignment.
  56.  
  57. Even though the operands are promoted in expressions, the compiler
  58. might not actually expand the arguments if the end results are the
  59. same.  In the example code, the compiler is free to perform the
  60. addition using a short accumulation register, assuming the result,
  61. when assigned to a short, is the same as would occur with promotion
  62. and conversion to short int.
  63.  
  64. :The reason I ask is that I am writing code that should be 
  65. :portable from a 16-bits environment (where short and int are the 
  66. :same size) to a 32-bits environment. (My 16-bits compiler does 
  67. :not complain about the code).
  68.  
  69. If the code is written correctly and assumptions are not made about
  70. the maximum values, e.g., that 1 << 16 == 0, you should be OK.
  71.  
  72. Thad
  73.